package com.lvyh.lightframe.log.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author lvyh 2021/03/21.
 */
@Aspect
@Component
@Order(0)
@Slf4j
public class ScheduleTaskLogAspect {
    private final String traceId = "traceId";

    @Before("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void before() {
        MDC.put(this.traceId, UUID.randomUUID().toString().replace("-", "").toLowerCase());
        log.info("Scheduling task aspect trigger...");
    }

    @After("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void after() {
        log.info("Scheduling task execution completed...");
        MDC.clear();
    }
}
